/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.FxCmsInfo;
import com.ykht.proj.coupon.entity.FxFavour;
import com.ykht.proj.coupon.mapper.FxCmsInfoMapper;
import com.ykht.proj.coupon.mapper.FxFavourMapper;
import com.ykht.proj.coupon.service.FxBrandPersonService;
import com.ykht.proj.coupon.service.FxFavourService;
import com.ykht.proj.coupon.vo.FavourVo;
import com.ykht.proj.coupon.vo.FxCmsInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 点赞记录表
 *
 * @author proj code generator
 * @date 2021-02-01 19:00:43
 */
@Service
@SuppressWarnings("all")
public class FxFavourServiceImpl extends ServiceImpl<FxFavourMapper, FxFavour> implements FxFavourService {

	@Autowired
	private FxBrandPersonService fxBrandPersonService;
	@Autowired
	private FxCmsInfoMapper fxCmsInfoMapper;
	@Autowired
	private FxFavourMapper fxFavourMapper;


	@Transactional(rollbackFor = Exception.class)
    @Override
    public void setGiveALike(FxCmsInfoVO fxCmsInfoVO) {


		Integer inviteType = 0;//邀请类型0文章
		Long waiterId = fxCmsInfoVO.getWaiterId();
		Long articleId = fxCmsInfoVO.getId();
		//注册饭友
		if (waiterId != null) fxBrandPersonService.inviteFY(inviteType, waiterId, articleId);

		Integer num = this.baseMapper.selectByPerId(fxCmsInfoVO);
		/*先判断用户是否对这片文章进行过点赞行为
    	* 没有就添加一条点赞记录
    	* 有就进行修改
    	* */
		if(num !=0){
			fxCmsInfoVO.setPerId(SecurityUtils.getPersonId());
			fxCmsInfoVO.setUpdateUser(fxCmsInfoVO.getCreateUser());
			fxCmsInfoVO.setUpdateTime(LocalDateTime.now());
			this.baseMapper.setGiveALike(fxCmsInfoVO);
//			Integer readCount = baseMapper.getReadCount(fxCmsInfoVO.getId());
			int count = this.count(Wrappers.lambdaQuery(FxFavour.class).eq(FxFavour::getArticleId, fxCmsInfoVO.getId()).eq(FxFavour::getPerId, SecurityUtils.getPersonId()).eq(FxFavour::getStatus,0));
			//添加点赞数
			fxCmsInfoMapper.updateStatisticsByCmsId(fxCmsInfoVO.getId(),0, 0, count>0?1:-1, 0, 0);
		}else {
			FxFavour fxFavour = new FxFavour();
			fxFavour.setPerId(fxCmsInfoVO.getCreateUser());
			fxFavour.setCreateUser(fxCmsInfoVO.getCreateUser());
			fxFavour.setCreateTime(LocalDateTime.now());
			fxFavour.setArticleId(fxCmsInfoVO.getId());
			fxFavour.setStatus(fxCmsInfoVO.getGiveALikeStatus());
			this.baseMapper.insert(fxFavour);
			//添加点赞数
			fxCmsInfoMapper.updateStatisticsByCmsId(fxFavour.getArticleId(),0, 0, 1, 0, 0);
		}
    }

	@Override
	public Page<FavourVo> showPage(Page page, FxFavour fxFavour) {
		return fxFavourMapper.showPage(page,fxFavour);
	}
}
